﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using FinanSoft.Utilities;

namespace FinanSoft.PresentacionAdministracion
{
    public partial class frmTaxConfiguration : Form
    {

        private int[] _taxIds;

        public frmTaxConfiguration()
        {
            InitializeComponent();
            fillTaxes();
        }

        private void fillTaxes()
        {
            int taxIndex = 0;
            List<Tax> taxList = ConfigurationManager.GetInstance().GetTaxes();
            _taxIds = new int[taxList.Count];
            cmbTax.Items.Clear();
            foreach (Tax tax in taxList)
            {
                _taxIds[taxIndex++] = tax.IdImpuesto;
                cmbTax.Items.Add(tax.Acronimo + " " + (tax.Porcentaje * 100).ToString() + " %");
            }
            if (cmbTax.Items.Count > 0)
                cmbTax.SelectedIndex = 0;
        }

        private bool validateInformation()
        {
            if (txtCode.Text.Trim().Length == 0 || txtName.Text.Trim().Length == 0 || txtPercentage.Text.Trim().Length == 0)
            {
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (cmbTax.SelectedIndex != Constants.INVALID_ID)
            {
                if (ConfigurationManager.GetInstance().DeleteTax(_taxIds[cmbTax.SelectedIndex]))
                {
                    MessageBox.Show(MessageConstants.TAX_DELETE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    fillTaxes();
                }
                else
                    MessageBox.Show(ConfigurationManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show(MessageConstants.NO_TAX_TO_DELETE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btnIngresar_Click(object sender, EventArgs e)
        {
            if (validateInformation())
            {
                if (ConfigurationManager.GetInstance().AddTax(txtName.Text, txtCode.Text, ((float)txtPercentage.Value) / 100))
                {
                    MessageBox.Show(MessageConstants.TAX_INSERTED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    fillTaxes();
                }
                else
                    MessageBox.Show(ConfigurationManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
